<template>
	<view>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">户主姓名：</view>
				<input :disabled="attestationStatus" v-model="ownerAttestation.homeName" placeholder="填写户主姓名"></input>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">小区：</view>
				<input v-model="ownerAttestation.areaName" placeholder="填写小区名称"></input>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">单元：</view>
				<input :disabled="attestationStatus" v-model="ownerAttestation.unitId" placeholder="填写单元"></input>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">门牌号：</view>
				<input :disabled="attestationStatus" v-model="ownerAttestation.hoseNumber" placeholder="填写门牌号"></input>
			</view>


		</form>
		<view class="cu-form-group margin-top">
			<!-- 认证成功才显示，目前的status时false -->
			<view v-if="!attestationStatus" class="title">是否认证：<span class="attestationResult">{{attestationResult}}</span></view>
		</view>

		<view class="register-commit">
			<button @click="requestAttestation" class='cu-btn bg-green shadow'>请求物业认证</button>
		</view>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex';
	export default {
		data() {
			return {
				index: -1,
				housings: [],
				picker: [],
				attestationResult: this.$store.state.userInfo.isAttestation == 1 ? "已认证" : this.$store.state.userInfo.isAttestation == 3?"等待审核":this.$store.state.userInfo.isAttestation == 2?"驳回,请联系物业":"未认证",
				attestationStatus: false, //暂时先显示出来，表示认证成功
				ownerAttestation: {
					homeName: this.$store.state.userInfo.home == null ? "" : this.$store.state.userInfo.home.homeName,
					areaName: this.$store.state.userInfo.home == null ? "" : this.$store.state.userInfo.home.areaName,
					unitId: this.$store.state.userInfo.home == null ? "" : this.$store.state.userInfo.home.unitId,
					hoseNumber: this.$store.state.userInfo.home == null ? "" : this.$store.state.userInfo.home.hoseNumber,
					telephone: this.$store.state.userInfo.telephone
				}
			};
		},
		methods: {
			...mapMutations(['login']),
			requestAttestation() {
				console.log(this.$store.state.userInfo);
				console.log(this.ownerAttestation);
				if(this.ownerAttestation.homeName==""||this.ownerAttestation.areaName==""||this.ownerAttestation.unitId==""||this.ownerAttestation.hoseNumber==""){
					uni.showToast({
						icon: 'none',
						title: '请完善所有信息再提交'
					});
					return
				}
				uni.request({
					url: this.baseurl + "user/attestation",
					method: "POST",
					data: this.ownerAttestation,
					success: (res) => {
						console.log(res.data.result.user)
						if (res.data.code == 200) {
							console.log("认证的时候",res.data.result.user)
							this.login(res.data.result.user)
							uni.showToast({
								icon: 'none',
								title: '提交成功，等待物业审核'
							});
							setTimeout(this.back(),500);
						} else if(res.data.code == 501){
							uni.showToast({
								icon: 'loading',
								title: res.data.msg
							})
						}
						else{
							uni.showToast({
								icon: 'success',
								title: '提交失败，请联系物业'
							});
						}
					}
				})
				// uni.request({
				// 	url:this.baseurl+'/owner/attestation',
				// 	method:'POST',
				// 	header: {
				// 		'content-type': 'application/x-www-form-urlencoded'
				// 	},
				// 	data:this.ownerAttestation,
				// 	success: (res) => {
				// 		if(res.data.code==200){
				// 			uni.showToast({
				// 				icon:'success',
				// 				title:'提交成功'
				// 			})
				// 			setTimeout(()=>{
				// 				uni.navigateBack()
				// 			},500)
				// 		}else{
				// 			uni.showToast({
				// 				icon:'loading',
				// 				title:'提交失败'
				// 			})
				// 		}
				// 	}
				// })
			},
			back() {
				uni.switchTab({
					url: "../user/user"
				})
			},
			refresh(){
				console.log(this.$store.state.userInfo.name);
				uni.request({
					url:this.baseurl+"user/getuser",
					data:{
						name:this.$store.state.userInfo.name,
					},
					success: (res) => {
						console.log(res)
						 this.login(res.data.result.user)
						 this.attestationResult= this.$store.state.userInfo.isAttestation == 1 ? "已认证" : this.$store.state.userInfo.isAttestation == 3?"等待审核":this.$store.state.userInfo.isAttestation == 2?"驳回,请联系物业":"未认证"
					}
				})
				setTimeout(() => {
					uni.stopPullDownRefresh()
				}, 1000);
			}
		},
		onBackPress() {
			this.back();
			return true;
		},
		onPullDownRefresh:function(){
			this.refresh();
		},
		onLoad:function(){
			this.refresh();
		}
	}
</script>

<style>
	@import url("../public/main.css");
	@import url("../public/icon.css");

	.attestationResult {
		color: #ED1C24
	}

	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
